/*-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License.You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
	
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-----------------------------------------------------------------------------*/

package fever.plugin.manager.models 
{
	import com.bourre.collection.HashMap;	import com.bourre.core.HashCodeFactory;	import com.bourre.events.EventChannel;	import com.bourre.model.AbstractModel;	import com.bourre.plugin.Plugin;		import fever.log.FeverDebug;	import fever.plugin.manager.RegistredPluginInfo;	import fever.plugin.manager.events.PluginActivationEvent;	import fever.plugin.manager.events.PluginManagerListener;	import fever.plugin.manager.events.PluginRegistrationEvent;		/**
	 * Plugin manager model.
	 * 
	 * <p>Plugins are registred / activated using their <code>communication 
	 * channel</code>.
	 * 
	 * @see fever.ioc.manager.AbstractManagerPlugin	 * @see fever.ioc.manager.events.PluginManagerEventList
	 * 
	 * //TODO Change doc after refactoring process
	 * 
	 * @author Romain Ecarnot
	 */
	public class PluginManagerModel extends AbstractModel
	{

		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		private var _pluginMap : HashMap;
		
		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Constructor.
		 * 
		 * @param owner Plugin owner.
		 */ 
		public function PluginManagerModel( owner : Plugin ) 
		{
			super( owner, PluginManagerModelList.PLUGIN_MANAGER_MODEL );
			
			_pluginMap = new HashMap( );
		}	
		
		/**
		 * Adds passed-in <code>listener</code> object for receiving all model 
		 * events.
		 * 
		 * @param listener PluginManagerListener object
		 * 
		 * @see fever.ioc.manager.events.PluginManagerListener
		 */
		public function addManagerListener( listener : PluginManagerListener ) : Boolean
		{
			return addListener( listener );
		}
		
		/**
		 * Removes passed-in <code>listener</code> object for receiving all model 
		 * events.
		 * 
		 * @param listener Listener object.
		 */
		public function removeManagerListener( listener : PluginManagerListener ) : Boolean
		{
			return removeListener( listener );
		}
		
		/**
		 * Registers a new plugin using passed-in plugin event.
		 * 
		 * <p>Fire an external event on <code>channel</code> event channel.<br />
		 * Connected views are notified. ( M.V.C. Pattern behaviour )</p>
		 * 
		 * @param event <code>PluginManagerEvent</code> event structure
		 */
		public function register( channel : EventChannel, data : RegistredPluginInfo = null ) : void
		{
			if( !_pluginMap.containsKey( channel ) )
			{
				var pData : Object = ( data == null ) ? HashCodeFactory.getNextKey() : data;
				
				_pluginMap.put( channel, pData );
				
				var eReg : PluginRegistrationEvent = new PluginRegistrationEvent( 
					PluginRegistrationEvent.onPluginRegistrationEVENT,
					true,
					channel,
					pData
				);
				
				getOwner().fireExternalEvent( eReg, channel );
				
				notifyChanged( eReg );
			}
		}
		
		/**
		 * Unregisters a new plugin using passed-in plugin event.
		 * 
		 * <p>Fire an external event on <code>channel</code> event channel.<br />
		 * Connected views are notified. ( M.V.C. Pattern behaviour )</p>
		 * 
		 * @param event <code>PluginManagerEvent</code> event structure
		 */
		public function unregister( channel : EventChannel ) : void
		{
			if( _pluginMap.containsKey( channel ) )
			{
				_pluginMap.remove( channel );
				
				var eReg : PluginRegistrationEvent = new PluginRegistrationEvent( 
					PluginRegistrationEvent.onPluginRegistrationEVENT,
					false,
					channel
				);
				
				//Send result to plugin	
				getOwner().fireExternalEvent( eReg, channel );
				
				notifyChanged( eReg );
			}
		}
		
		/**
		 * Activates plugin using passed-in his <code>channel</code>. 
		 * 
		 * <p>Fire an external event on <code>channel</code> event channel.<br />
		 * Connected views are notified. ( M.V.C. Pattern behaviour )</p>
		 * 
		 * @param event <code>PluginManagerEvent</code> event structure
		 */
		public function activate( channel : EventChannel ) : void
		{
			FeverDebug.WARN( "PluginManager activate " + channel );
			
			if( _pluginMap.containsKey( channel ) )
			{
				var event : PluginActivationEvent = new PluginActivationEvent( 
					PluginActivationEvent.onPluginActivationEVENT,
					true,
					channel
				);
				getOwner().fireExternalEvent( event, channel );
				
				notifyChanged( event );
			}
			else getLogger().error( 
				channel + " plugin is not available in current context" 
			);
		}
		
		/**
		 * Desactivate plugin using passed-in his <code>channel</code>. 
		 * 
		 * <p>Fire an external event on <code>channel</code> event channel.<br />
		 * Connected views are notified. ( M.V.C. Pattern behaviour )</p>
		 * 
		 * @param channel <code>EventChannel</code> of plugin to desactivate
		 */
		public function desactivate( channel : EventChannel ) : void
		{
			FeverDebug.WARN( "PluginManager desactivate " + channel );
			
			if( _pluginMap.containsKey( channel ) )
			{
				var event : PluginActivationEvent = new PluginActivationEvent( 
					PluginActivationEvent.onPluginActivationEVENT,
					false,
					channel
				);
				getOwner().fireExternalEvent( event, channel );
				
				notifyChanged( event );
			}
			else getLogger().error( 
				channel + " plugin is not available in current context" 
			);
		}			
	}
}
